apiready = function(){
$(function() {
    var a = $("#cartBody");	
    var c = $("#divNone");
    var b = function() {
        var o = "";
        var h = $("#divTopMoney");
        var g = $("#divBtmMoney");
        
        var e = function(t, s, r, q) {
            $.PageDialog.fail(t, s, r, q);
        };
        var n = function(s, r, q) {
            $.PageDialog.confirm(s, r, q);
        };
        if (h.length > 0) {
            h.children("a").click(function() {
              //付款页面
              if(!getUserinfo()){
                  //$.PageDialog.fail('请登录用户');
				  localData.remove('reload');
                   randomSwitchBtn('login',api);
				  return false;
              }
              //randomSwitchBtn('payment',api);
              api.openWin({
                  name : 'z_payment',
                  url : './z_payment.html',
                  reload : true,
                  delay : 2,
              });
            });
        }
        g.children("a").click(function() {
              if(!getUserinfo()){
					localData.remove('reload');
                   randomSwitchBtn('login',api);
				  //$.PageDialog.fail('请登录用户');
                  return false;
              }	
              api.openWin({
                  name : 'z_payment',
                  url : './z_payment.html',
                  reload : true,
                  delay : 2,
              });
            //randomSwitchBtn('payment',api);
            //付款页面
        });
      var cart = localData.get('z_cart');
      if(!cart){
          $('#divNone').show();
          return;
      }
      

          
        setInterval(function(){
            if(!localData.get('z_cart_num')){
            $('.g-Cart-list').hide();
            $('#divNone').show();
            }
        },1000);
        var zclearCart = document.getElementById('zclearCart');
        zclearCart.onclick = function(){
            api.confirm({
                title: '提示',
            msg: '是否清空购物车？',
            buttons: ['确定', '取消']
        },function( ret, err ){
            if( ret ){
                if(ret['buttonIndex'] == 1){
                    localData.remove('z_cart_num');
                    localData.remove('z_cart');
                    $('.g-Cart-list').hide();
                    $('#divNone').show();
                }else{
                    //alert(JSON.stringify( err ));
                }
            }else{
                 //alert( JSON.stringify( err ) );
                }
            });
        };
        var func = 'funcA();';
        api.execScript({
            name:'root',
            script:func
        });
                  
        if(api.connectionType == 'none'){
            api.showProgress({
                 title : '未连接网络',
                 text : ''
             });
            return false;
        }else{
            api.showProgress({title:'加载中',text:''});
            setTimeout(function(){
                api.hideProgress();
            },15000);
        } 
      
      PostData(config.url+'/app.php?act=zcartlist',{cart:cart},function(data){
          api.hideProgress();
          if(data['shop'] == 1){
              var html = '';
              var countCart = 0;
              for(var i in data['shoplist']){
                     countCart++;
                     html += '<li class="lis"><a class="fl u-Cart-img" href="javascript:openItem(\'z_item\','+data['shoplist'][i]['id']+',function(id){localData.set(\'z_goods_id\',id);});">';
                     html += '<img class="lazy" style="border-radius:4px;" data-original="'+config.upload_url+'/'+data['shoplist'][i]['thumb']+'" border="0" alt=""/></a><div class="u-Cart-r">';
                     html += '<p class="z-Cart-tt"><a href="javascript:openItem(\'z_item\','+data['shoplist'][i]['id']+',function(id){localData.set(\'z_goods_id\',id);});" class="gray6">'+data['shoplist'][i]['title']+'</a></p>';
                     var freight = '<span style="float:right;" count="'+data['shoplist'][i]['cart_freight']+'">运费：'+data['shoplist'][i]['cart_freight']+'</span>';
                     html += '<ins class="z-promo gray9">库存<em class="arial">'+data['shoplist'][i]['zongrenshu']+'</em>件'+freight+'</ins>';
                     html += '<p class="gray9">购买数量：<em class="arial">'+data['shoplist'][i]['cart_gorenci']+'</em>件/<em class="orange arial lineCount" count="'+data['shoplist'][i]['cart_xiaoji']+'">￥'+data['shoplist'][i]['cart_xiaoji']+'</em></p>';
                     html += '<p class="f-Cart-Other">';
                     html += '<a href="javascript:;" class="fr z-del delgoods" name="delLink" cid="'+data['shoplist'][i]['id']+'"></a>';
                     if(data['shoplist'][i]['cart_gorenci'] == 1){
                         html += '<a href="javascript:;" class="fl z-jian z-jiandis">-</a>';
                     }else{
                         html += '<a href="javascript:;" class="fl z-jian">-</a>';
                     }
                     html += '<input id="txtNum'+data['shoplist'][i]['id']+'" name="num" data-money="'+data['shoplist'][i]['money']+'" freight="'+data['shoplist'][i]['yunjiage']+'" type="text" maxlength="7" value="'+data['shoplist'][i]['cart_gorenci']+'" class="fl z-amount" style="height:28px;" />';
                     if(data['shoplist'][i]['cart_gorenci'] == data['shoplist'][i]['zongrenshu']){
                         html += '<a href="javascript:;" class="fl z-jia z-jiadis">+</a>';
                     }else{
                         html += '<a href="javascript:;" class="fl z-jia">+</a>';
                     }
                     html += '<input type="hidden" value="'+data['shoplist'][i]['cart_gorenci']+'" />';
                     html += '<input type="hidden" value="'+data['shoplist'][i]['zongrenshu']+'" />';
                     html += '</p></div></li>';
             }
              $('#cartBody').append(html);
              $('.countCart').text(countCart);
              $('.countPrice').text(data['MoenyCount']);
              $('.g-Cart-list').show();
              lyzimg();
              
             $("a[name=delLink]", a).each(function(q) {
                 $(this).bind("click",function() {
                     var r = $(this);
                     var t = r.attr("cid");
                     var s = function() {
                         if (z_deleteCart(t)) {
                                 r.parent().parent().parent().remove();
                                 setTimeout(m,450);
                                 f();
                             } else {
                                 e("删除失败，请重试");
                             }
                     };
                     n("您确定要删除吗？", s);
                 });
             });
             
     var m = function() {
         var q = 0;
         var r = 0;
         $("input:text[name=num]", a).each(function(s) {
             var t = parseInt($(this).val());
             if (!isNaN(t)) {
                 r++;
                 q += t;
             }
         });
         
         if (r > 0) {
             if (h.length > 0) {
                 h.children("span").html(q + ".00");
             }
             q = 0;
             $('li.lis').each(function(e){
                 var freight = $(this).find('ins span').attr('count');
                 var lineCount = $(this).find('em.lineCount').attr('count');
                 q += parseInt(freight)+parseInt(lineCount);
             });
             g.children("p").html('总共购买<span class="orange arial z-user">' + r + '</span>个商品  合计金额：<span class="orange arial">' + q + ".00</span> 元");
         } else {
             g.remove();
         }
     };
     
     var d = function() {
         var z = $(this);
         var t = z.attr("id");
         var v = t.replace("txtNum", ""); //商品id
         var q = z.next().next();
         var r = parseInt(z.next().next().next().val());
         var s, y, w = /^[1-9]{1}\d{0,6}$/;
         var u;
         o = t;
         var x = function() {
             if (o != t) {
                 return;
             }
             s = q.val();
             y = z.val();
             if (y != "" && s != y) {
                 var B = $(window).width();
                 var A = (B) / 2 - z.offset().left - 127;
                 if (w.test(y)) {
                     u = parseInt(y);
                     if (u <= r) {
                         q.val(y);
                     } else {
                         u = r;
                         e("最多" + u + "人次", z, -75, A);
                         z.val(u);
                         q.val(u);
                     }
                     p(u, z);
                     j(z, v, u);
                     i(z, u, r);
                     setTimeout(m,450);
                 } else {
                     e("只能输正整数哦", z, -75, A);
                     z.val(s);
                 }
             }
             setTimeout(x, 200);
         };
         x();
     };
     
     var p = function(r, u) {
         var t = u.parent().parent().parent();
         var q = t.find("div.z-Cart-tips");
         if (r > 100) {
             if (q.length == 0) {
                 //var s = $('<div class="z-Cart-tips">已超过100人次，请谨慎参与！</div>');
                 //t.prepend(s);
             }
         } else {
             q.remove();
         }
     };
     
     var l = function() {
         var q = $(this);
         if (o == q.attr("id")) {
             o = "";
         }
         if (q.val() == "") {
             q.val(q.next().next().val());
         }
     };
     
     var j = function(q, t, r) {
         var s = function(w) {
             if (w.code == 1) {
                 var v = $(window).width();
                 var u = (v) / 2 - q.offset().left - 127;
                 e("本期商品已购买光了", q, -75, u);
             } else {
                 
                 var money = parseInt(q.attr('data-money'));
                 if (w.code == 0) {
                     //0元购
                     if(money == 0){
                         var freight = q.attr('freight');
                         q.parent().prev().prev().find('span').attr('count',r*freight).text('运费：'+r*freight);
                     }
                     var _count = r*money;
                     q.parent().prev().html('总共购买：<em class="arial">' + r + '</em>件/小计<em class="orange arial lineCount" count="'+_count+'">￥' + _count + ".00</em>");
                 }

                 if(!isNaN(money)){
                     z_addCart(t,money,r,'cart');
                 }
             }
         };

         GetJPData(config.url+'/app.php?act=checkshopcart',{goods_id:t},s);
                  
     };
     
     var k = function(w, v) {
         var u = v.attr("id");
         var s = u.replace("txtNum", ""); //商品id
         var r = parseInt(v.next().next().next().val()); //库存
         var q = v.next().next();
         var t = parseInt(q.val()) + w; //数量
         if (t > 0 && t <= r) {
             i(v, t, r);
             q.val(t);
             v.val(t);
             p(t, v);
             j(v, s, t);
             setTimeout(m,450);
         }
     };
     
     var i = function(r, t, s) {
         var q = r.prev();
         var u = r.next();
         q.removeClass("z-jiandis");
         if (s == 1) {
             q.addClass("z-jiandis");
             u.addClass("z-jiadis");
         } else {
             if (t == 1) {
                 q.addClass("z-jiandis");
                 u.removeClass("z-jiadis");
             } else {
                 if (t == s) {
                     q.removeClass("z-jiandis");
                     u.addClass("z-jiadis");
                 } else {
                     q.removeClass("z-jiandis");
                     u.removeClass("z-jiadis");
                 }
             }
         }
     };
     
     $("input:text[name=num]", a).each(function(q) {
         var r = $(this);
         r.bind("focus", d).bind("blur", l);
         r.prev().bind("click",function() {
             k(-1, r);
             
         });
         r.next().bind("click",function() {
             k(1, r);
         });
     });
     
     var f = function() {
         var q = $("li", "#cartBody");
         if (q.length < 1) {          
             a.parent().remove();                
             c.show();
         } else {
             if (q.length < 4) {
                 h.remove();
             }
         }
     };
          }else{
              $('#divNone').show();
          }
       
    	  
    	  
    	  
      });//postData end

    };
	 
    if (a.length > 0) {
        b();
    } else {
        c.show();
    }
});
};